﻿using QuickChat_Service.Repositories.Database;
using QuickChat_Service.Repositories.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuickChat_Service.Repositories.Repositories.Implementations
{
    public class EFTalkerRepository : EFRepository<Talker>, ITalkerRepository
    {

        public EFTalkerRepository(QuickChatApplicationEntities context) : base(context) { }

        #region Interface Implementation

        public void LeaveChatRoom(int userId)
        {
            Talker talker = DbSet.Find(userId);
            talker.CurrentRoomId = null;

            base.Update(talker, userId);
        }

        public void EnterChatRoom(int userId, int chatRoomId)
        {
            Talker talker = DbSet.Find(userId);
            talker.CurrentRoomId = chatRoomId;

            base.Update(talker, userId);
        }

        public void CreateNewTalker(string talkerAlias)
        {
            base.Add(new Talker()
            {
                ChatAlias = talkerAlias
            });
        }

        public IQueryable<Talker> GetTalkersByRoom(int roomId)
        {
            return DbSet.Where(talker => talker.CurrentRoomId == roomId).AsQueryable();
        }

        public IQueryable<Talker> GetTalkersBySession(int sessionId)
        {
            return DbContext.Set<ChatSession>().Find(sessionId).Talkers.AsQueryable();
        }

        public Talker GetByAlias(string alias)
        {
            return DbSet.SingleOrDefault(a => a.ChatAlias == alias);
        }

        #endregion
    }
}